#include "bu_inter.h"
#include "fu_inter.h"
#include "string.h"
#include "ha_485.h"
#include "bu_can.h"
#include "other.h"


static st_inter inter;
static struct RES_485 res485;
static uint8_t rx_buf[UART4_MAX];
static uint8_t tx_buf[UART4_MAX];
static uint16_t tx_num = 0;

static void inter_init(void)
{
	inter_hardware_init();
}

static uint8_t inter_rx_charge(uint8_t *p_buf,uint8_t len)
{
	uint8_t res_temp = 0;
	uint8_t temp_sum = 0;
	uint8_t temp_sum1 = 0;
	struct FRAME_R485 *p_rframe = NULL;
	struct FRAME_T485 *p_tframe = NULL;
	
	p_tframe = (struct FRAME_T485 *)tx_buf;
	p_rframe = (struct FRAME_R485 *)p_buf;
	
	if((p_rframe->len + 8) != len){
		return 0;
	}
	if((p_rframe->head_H != PRO_HEAD_H)||(p_rframe->head_L != PRO_HEAD_L)){
		return 0;
	}
	temp_sum = cal_sum_byte((uint8_t *)p_rframe,6);
	temp_sum1 = cal_sum_byte((uint8_t *)&p_rframe->ack,p_rframe->len);
	if((temp_sum != p_rframe->sum)||(temp_sum1 != p_rframe->sum1)){	
		return 0;
	}
	if(inter.ack_flag == 1){
		if((inter.ack_sn0 == p_rframe->ack.a_sn0) && (inter.ack_sn1 == p_rframe->ack.a_sn1) && (inter.ack_cmd == p_rframe->ack.a_cmd)){
			inter.ack_flag = 0;
			inter.res_send_cnt = 0;
			tx_num++;
		}
	}else{
		tx_num++;
	}
	switch(p_rframe->cmd){
		case PRO_INFO:
			
		break;
		case PRO_CALL:
			inter.res_flag = 1;
			
		break;
		case PRO_OPENDOOR:
			inter.res_flag = 1;
			
		break;
		case PRO_CLOSEDOOR:
			inter.res_flag = 1;
			
		break;
		default :
		break;
	}
	p_tframe->head_H = PRO_HEAD_H;
	p_tframe->head_L = PRO_HEAD_L;
	p_tframe->sn_H = tx_num>>8;
	p_tframe->sn_L = tx_num&0xff;
	p_tframe->cmd = PRO_VB;
	p_tframe->len = 36;
	
	p_tframe->ack.a_len = 3;
	p_tframe->ack.a_sn0 = p_rframe->sn_H;
	p_tframe->ack.a_sn1 = p_rframe->sn_L;
	p_tframe->ack.a_cmd = p_rframe->cmd;
	
	if(inter.res_flag == 1){
		res_temp = p_lift->get_ack((uint8_t *)&res485);
		if(res_temp == 1){
			inter.res_flag = 0;
			inter.res_send_cnt = 5;
			inter.ack_flag = 1;
			inter.ack_sn0 = tx_num>>8;
			inter.ack_sn1 = tx_num&0xff;
			inter.ack_cmd = PRO_VB;
		}
	}
	if(inter.res_send_cnt > 0){
		inter.res_send_cnt--;
		memcpy((uint8_t *)&p_tframe->res,(uint8_t *)&res485,sizeof(struct RES_485));
	}else{
		inter.ack_flag = 0;
		memset((uint8_t *)&p_tframe->res,0,sizeof(struct RES_485));
		p_tframe->res.r_len = 7;
	}
	p_lift->get_lift_data((uint8_t *)&p_tframe->data);
	temp_sum = cal_sum_byte((uint8_t *)p_tframe,6);
	p_tframe->sum = temp_sum;
	temp_sum1 = cal_sum_byte((uint8_t *)&p_tframe->res,p_tframe->len);
	p_tframe->sum1 = temp_sum1;
	
	
	return 1;

}

static void inter_rx_deal(void)
{
	uint8_t res = 0;
	uint8_t recv_len = 0;
	struct FRAME_R485 *p_tframe = NULL;
	p_tframe = (struct FRAME_R485 *)tx_buf;
	
	recv_len = get_uart4_data(rx_buf);
	if(recv_len > 0){
		res = inter_rx_charge(rx_buf,recv_len);
		if(res == 1){
			send_uart4_data((uint8_t *)p_tframe,p_tframe->len+8);
		}
	}
}

static void inter_conver_deal(void)
{
	if(st_485.status == SENDEND){
		if(st_485.conversion_time > 0){
			st_485.conversion_time--;
		}else{
			set_inter_rx_mode();
			st_485.status = RECVING;
		}
	}
}




pst_inter p_inter;
void inter_business_init(void)
{
	inter.init = inter_init;
	inter.conver = inter_conver_deal;
	inter.recv_data_deal = inter_rx_deal;
	
	inter.init();
	
	p_inter = &inter;
}

